package com.carl.zlds.controller;

import com.carl.zlds.tool.common.result.Result;
import com.carl.zlds.annotation.Login;
import com.carl.zlds.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author carl
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(tags = {"文件管理"})
public class FileController {

    private final FileUtil fileUtil;

    public FileController(FileUtil fileUtil) {
        this.fileUtil = fileUtil;
    }

    @Value("${fdfs.tracker}")
    private String tracker ;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    @Login(needToken = false)
    public Result<String> uploadFileToFast(@RequestParam("file") MultipartFile file) throws IOException {
        String path = fileUtil.uploadFile(file);
        return Result.success(tracker + path);
    }

    @ApiOperation("富文本上传文件")
    @PostMapping("/editor-upload")
    @Login(needToken = false)
    public EditorResult<List<String>> editorUploadFileToFast(@RequestParam("file") MultipartFile file) throws IOException {
        List<String> pathList = new ArrayList<>();
        String path = fileUtil.uploadFile(file);
        pathList.add(tracker + path);
        return EditorResult.success(pathList);
    }
}